package org.tulkas.web;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
import org.tulkas.base.ITulkasApplicationContext;

/**
 * @author <a href="mailto:tulkas.web@gmail.com">Tulkas</a>
 *
 */
public class TulkasVelocityConfigurer extends VelocityConfigurer {
	public final static String TULKAS_CLASSPATH_RESOURCE_LOADER = "tulkasClassPath";

	@Autowired
	private ITulkasApplicationContext appContext;

	@Override
	protected void postProcessVelocityEngine(VelocityEngine velocityEngine) {
		super.postProcessVelocityEngine(velocityEngine);
		if (appContext.isDebug()) {
			velocityEngine.setProperty(TULKAS_CLASSPATH_RESOURCE_LOADER
					+ ".resource.loader.cache", "false");
			velocityEngine.setProperty(
					RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "false");
			velocityEngine.setProperty(RuntimeConstants.VM_LIBRARY_AUTORELOAD,
					"true");
		} else {
			velocityEngine.setProperty(TULKAS_CLASSPATH_RESOURCE_LOADER
					+ ".resource.loader.cache", "true");
			velocityEngine.setProperty(
					RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true");
			velocityEngine.setProperty(RuntimeConstants.VM_LIBRARY_AUTORELOAD,
					"false");
		}
	}
}
